package com.adee.juc;

import com.adee.util.Utils;

import java.util.concurrent.Semaphore;

/**
 * 使用信号量Semaphore模拟停车场进出车辆，假设总共10个停车位
 */
public class Test007_Semaphore {
    public static void main(String[] args) {
        Semaphore sem = new Semaphore(10);
        Runnable in = ()->{
            for (;;) {
                Utils.sleep(1);
                try {
                    sem.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("===>>>进去一辆车");
            }
        };
        Runnable out = ()->{
            for (;;) {
                Utils.sleep(2);
                sem.release();
                System.out.println("<<<===出来一辆车");
            }
        };
        new Thread(in).start();
        new Thread(out).start();
    }
}
